<template>
    <view class="login-container" :style="{ 'justify-content': loginType ? 'center' : '' }">
        <!-- 圆形Logo -->
        <button open-type="chooseAvatar" @chooseavatar="bindchooseavatar">
            <view class="logo-container">
                <image :src="formData.avatar" mode="aspectFit" class="logo"></image>
            </view>
        </button>


        <!-- 表单区域 -->
        <view class="form-container">
            <view class="input-group">
                <input v-model="formData.nickName" type="nickname" placeholder="请输入昵称" maxlength="10" class="input" />
            </view>
            <view class="input-group">
                <input v-model="formData.phonenumber" type="number" placeholder="请输入手机号" maxlength="11" class="input" />
            </view>
        </view>

        <!-- 协议勾选 (固定在底部) -->
        <view class="agreement-footer">
            <button class="login-btn" :class="{ active: canSubmit }" :disabled="!canSubmit" @click="submit">
                提交
            </button>
        </view>
    </view>

</template>

<script setup>
import { computed, ref } from 'vue'
import { onLoad } from '@dcloudio/uni-app'
import { BASE_URL } from '@/config/init'
import { upDateUser } from '@/api/apiList'

const formData = ref({
    nickName: '',
    phonenumber: '',
    avatar: ''
})

const setForm = () => {
    let userInfo = JSON.parse(uni.getStorageSync('userInfo'))
    formData.value.nickName = userInfo.nickName
    formData.value.phonenumber = userInfo.phonenumber
    formData.value.avatar = userInfo.avatar
}

const bindchooseavatar = (e) => {
    formData.value.avatar = e.detail.avatarUrl
    uploadAvatar()
}

const uploadAvatar = () => {
    return new Promise((resolve, reject) => {
        uni.uploadFile({
            url: BASE_URL + '/common/upload', // 上传图片的服务器地址
            filePath: formData.value.avatar,
            name: "file",
            header: {
                'Authorization': 'Bearer ' + uni.getStorageSync('token'),
                'Content-Type': 'multipart/form-data'
            },
            success: function (res) {
                formData.value.avatar = JSON.parse(res.data).url
                resolve(JSON.parse(res.data).url)
            },
            fail: function (res) {
                uni.showToast({
                    title: '上传失败',
                    icon: 'none',
                })
                reject(res)
            }
        });
    })
}

const submit = async () => {
    uni.showLoading({
        title: '提交中'
    })
    const res = await upDateUser(formData.value)
    if (res.code == 200) {
        uni.hideLoading()
        uni.showToast({
            title: '修改成功',
            icon: 'none',
        })
        setTimeout(() => {
            uni.navigateBack()
        }, 500)
    } else {
        uni.hideLoading()
        uni.showToast({
            title: res.msg,
            icon: 'none',
        })
    }
}

onLoad(() => {
    setForm()
})

// 计算属性：是否可以提交
// && formData.value.phonenumber && /^1[3-9]\d{9}$/.test(formData.value.phonenumber)
const canSubmit = computed(() => {
    return (
        formData.value.nickName
    )
})
</script>

<style lang="scss" scoped>
.login-container {
    padding: 60rpx 60rpx 140rpx;
    display: flex;
    flex-direction: column;
    align-items: center;
    min-height: 100vh;
    box-sizing: border-box;
    position: relative;
}

.logo-container {
    width: 160rpx;
    height: 160rpx;
    border-radius: 50%;
    overflow: hidden;
    background-color: #f5f5f5;
    margin-bottom: 80rpx;

    .logo {
        width: 100%;
        height: 100%;
    }
}

.form-container {
    width: 100%;
}

/* 手机号输入组 */
.phone-input-group {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 40rpx;


    .phone-input {
        flex: 1;
        height: 90rpx;
        padding: 0 30rpx;
        font-size: 28rpx;
        border: 1px solid #e5e5e5;
        border-radius: 12rpx;
        overflow: hidden;
        width: 100%;
    }

    .divider {
        width: 1px;
        height: 40rpx;
        background-color: #e5e5e5;
    }

    .send-btn {
        height: 90rpx;
        margin-left: 10rpx;
        line-height: 90rpx;
        padding: 0;
        background-color: transparent;
        color: #55946d;
        font-size: 24rpx;
        border: none;
        text-align: right;

        &::after {
            border: none;
        }

        &[disabled] {
            color: #999;
        }
    }
}

/* 验证码输入框 */
.input-group {
    margin-bottom: 60rpx;

    .input {
        height: 90rpx;
        padding: 0 30rpx;
        border: 1px solid #e5e5e5;
        border-radius: 12rpx;
        font-size: 28rpx;
    }
}

/* 登录按钮 */
.login-btn {
    width: 100%;
    height: 90rpx;
    line-height: 90rpx;
    background-color: #cccccc;
    color: white;
    border-radius: 12rpx;
    font-size: 32rpx;
    border: none;

    &.active {
        background-color: #55946d;
    }

    &[disabled] {
        background-color: #cccccc !important;
    }
}

/* 底部协议 */
.agreement-footer {
    position: fixed;
    bottom: 30rpx;
    left: 0;
    right: 0;
    padding: 0 60rpx;
    font-size: 24rpx;
    color: #666;
    text-align: center;


    .link {
        color: #55946d;
        margin: 0 6rpx;
    }
}

/* 自定义圆形复选框 */
checkbox {
    border-radius: 50%;
    transform: scale(0.8);
    margin-right: 8rpx;
}

.login-type {
    text-align: right;
    color: #55946d;
    font-size: 24rpx;
    margin-top: 30rpx;
}

button {
    margin: 0;
    padding: 0;
    line-height: 1;
    background-color: transparent;
    border: none;
}

button::after {
    border: none;
}
</style>
